﻿using Ext.Net;
using System;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using NOAH.CMS.Repository;
using NOAH.CMS.Cache;
using NOAH.CMS.Infracstructure;

public partial class admin_Modules_Subject_AddOrEdit : System.Web.UI.Page
{
    private string id = string.Empty;

    public void AddOrEdit(object sender, DirectEventArgs e)
    {
        try
        {
            using (SubjectRepository repository = new SubjectRepository())
            {
                Subject entity = null;
                if (this.id != "0")
                {
                    entity = repository.Get(int.Parse(this.id));
                }
                else
                {
                    entity = new Subject();
                }
                entity.Name = txtName.Text;
                entity.Content = e.ExtraParams["fckParameter"].ToString();
                entity.IsOnTopMenu = chkInOnTop.Checked;
                if (this.id == "0")
                {
                    repository.Add(entity);
                }
                repository.SubmitChanges();

                if (this.id != "0")
                {
                    BasePage.RegisterCallBackFromChild(this.ScriptManager2, string.Empty);
                    BasePage.RegisterCloseWindow(this.ScriptManager2);
                }
                else
                {
                    Resetcontrol();
                }

                ExtNet.Msg.Hide();
                Util.ShowMessage(Constants.UPDATE_SUCCESS);
                new SubjectCacheLayer().InvalidateCache();
                BasePage.RegisterCallBackFromChild(this.ScriptManager2, string.Empty);
            }
        }
        catch
        {
            Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
    }

    private void Resetcontrol()
    {
        txtName.Reset();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.Request.QueryString["id"] ?? "0";

        this.txtName.Focus(false, 100);
        if (!ExtNet.IsAjaxRequest)
        {
            this.ShowDetail();
        }
    }

    private void ShowDetail()
    {
        if (this.id != "0")
        {
            using (SubjectRepository contactRepositiory = new SubjectRepository())
            {
                Subject entity = contactRepositiory.Get(int.Parse(this.id));
                txtName.Text = entity.Name;
                fckHtmlEditor.Text = entity.Content;
                chkInOnTop.Checked = entity.IsOnTopMenu ?? false;
            }
        }
    }

}